package com.colin.app.activity.view;

import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.colin.app.app.AppActivity;
import com.colin.app.app.BasePresenterImpl;
import com.colin.app.bean.DemoBean;
import com.colin.app.bean.ItemBean;
import com.colin.library.help.StringHelp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Colin on 2017/2/7.
 */

public class ViewPresenterImpl extends BasePresenterImpl implements ViewContract.ViewPresenter {

    private ViewContract.ViewView mViewView;
    private ViewContract.ViewModel mViewModel;

    public ViewPresenterImpl(AppActivity mAppActivity, ViewContract.ViewView mViewView, DemoBean demoBean) {
        super(mAppActivity, mViewView);
        this.mViewView = mViewView;
        this.mViewModel = new ViewModelImpl(demoBean);
    }

    @Override
    public void setData() {
        mViewView.setTitle(mViewModel.getTitle());
    }

    @Override
    public void editTextChanged(String text, ImageView image_delete_password) {
        image_delete_password.setVisibility(StringHelp.isEmpty(text) ? View.INVISIBLE : View.VISIBLE);
    }

    @Override
    public void editPasswordShow(ImageView image_password_hide, EditText edit_password) {
        boolean isSelected = image_password_hide.isSelected();
        image_password_hide.setSelected(!isSelected);
        if (isSelected) {
            edit_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else if (!isSelected) {
            edit_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }

    @Override
    public void editDeleteText(EditText edit_password) {
        edit_password.setText("");
    }

    @Override
    public List<ItemBean> listGetItemData(boolean isRefresh) {

        int page = isRefresh ? 1 : mViewModel.getPage();
        List<ItemBean> itemBeanList = new ArrayList<>();
        itemBeanList.addAll(mViewModel.getItemBeanList(page));
        mViewModel.setPage(page + 1);
        return itemBeanList;
    }

    @Override
    public String getWebViewUrl() {
        return mViewModel.getWebViewUrl();
    }



    @Override
    public void destroy() {
        if (null!=mViewModel){
            mViewModel.destroy();
        }
        super.destroy();
    }
}
